package com.tqk.taxismart.list;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.tqk.constant.Constant;
import com.tqk.taxismart.obj.Taxi;

public class TaxiAdapterEstimate extends BaseAdapter {

	Context mContext;
	List<Taxi> data;
	ListTaxiInterface handler;

	public TaxiAdapterEstimate(Context ctx, List<Taxi> list, ListTaxiInterface handler) {
		mContext = ctx;
		data = list;
		handler = handler;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(final int position, View converView, ViewGroup viewParent) {
		View view = converView;
		Holder holder = new Holder();
		if (view == null) {
			LayoutInflater inf = ((Activity)mContext).getLayoutInflater();
			view = inf.inflate(com.tqk.taxismart.R.layout.taxi_row_item_estimate, null);
			holder.imgIcon = (ImageView)view.findViewById(com.tqk.taxismart.R.id.imgIcon_id);
//			holder.txtChair = (TextView)view.findViewById(com.tqk.taxismart.R.id.txtNumberChair_id);
			holder.txtName = (TextView)view.findViewById(com.tqk.taxismart.R.id.txtNameTaxi_id);
			holder.imgStar1 = (ImageView)view.findViewById(com.tqk.taxismart.R.id.imgStar1_id);
			holder.imgStar2 = (ImageView)view.findViewById(com.tqk.taxismart.R.id.imgStar2_id);
			holder.imgStar3 = (ImageView)view.findViewById(com.tqk.taxismart.R.id.imgStar3_id);
			holder.imgStar4 = (ImageView)view.findViewById(com.tqk.taxismart.R.id.imgStar4_id);
			holder.imgStar5 = (ImageView)view.findViewById(com.tqk.taxismart.R.id.imgStar5_id);
			holder.callTaxi = (Button)view.findViewById(com.tqk.taxismart.R.id.btnCallLine_id);
			holder.txtPrice = (TextView)view.findViewById(com.tqk.taxismart.R.id.txtPrice_id);
			view.setTag(holder);
		}else{
			holder = (Holder)view.getTag();
		}
			holder.imgIcon.setBackgroundResource(com.tqk.taxismart.R.drawable.ic_launcher);
			holder.txtName.setText(data.get(position).getNameTaxi());
//			holder.txtChair.setText(data.get(position).getseatCount());
			float estimatePrice = Float.valueOf(Constant.DISTANCE.trim()) * data.get(position).getPrice2();
			estimatePrice = (float)Math.round(estimatePrice*100)/100;
			holder.txtPrice.setText("Estimate Giá:" + estimatePrice +"000vnd");
			holder.callTaxi.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					handler.actionCallNumber(data.get(position).getCallLine());
				}
			});
			
			switch (data.get(position).getRateCount()) {
				case 1:
					holder.imgStar1.setVisibility(View.VISIBLE);
					holder.imgStar2.setVisibility(View.INVISIBLE);
					holder.imgStar3.setVisibility(View.INVISIBLE);
					holder.imgStar4.setVisibility(View.INVISIBLE);
					holder.imgStar5.setVisibility(View.INVISIBLE);
					break;
				case 2:
					holder.imgStar1.setVisibility(View.VISIBLE);
					holder.imgStar2.setVisibility(View.VISIBLE);
					holder.imgStar3.setVisibility(View.INVISIBLE);
					holder.imgStar4.setVisibility(View.INVISIBLE);
					holder.imgStar5.setVisibility(View.INVISIBLE);
					break;
	
				case 3:
					holder.imgStar1.setVisibility(View.VISIBLE);
					holder.imgStar2.setVisibility(View.VISIBLE);
					holder.imgStar3.setVisibility(View.VISIBLE);
					holder.imgStar4.setVisibility(View.INVISIBLE);
					holder.imgStar5.setVisibility(View.INVISIBLE);
					break;
	
				case 4:
					holder.imgStar1.setVisibility(View.VISIBLE);
					holder.imgStar2.setVisibility(View.VISIBLE);
					holder.imgStar3.setVisibility(View.VISIBLE);
					holder.imgStar4.setVisibility(View.VISIBLE);
					holder.imgStar5.setVisibility(View.INVISIBLE);
					break;
	
				case 5:
					holder.imgStar1.setVisibility(View.VISIBLE);
					holder.imgStar2.setVisibility(View.VISIBLE);
					holder.imgStar3.setVisibility(View.VISIBLE);
					holder.imgStar4.setVisibility(View.VISIBLE);
					holder.imgStar5.setVisibility(View.VISIBLE);
					break;
	
				default:
					break;
					
					
			}
			holder.imgStar1.invalidate();
			holder.imgStar2.invalidate();
			holder.imgStar3.invalidate();
			holder.imgStar4.invalidate();
			holder.imgStar5.invalidate();
		return view;
	}
	
	
	static class Holder{
		private ImageView imgIcon;
		private TextView txtName;
		private ImageView imgStar1,imgStar2,imgStar3,imgStar4,imgStar5;
		private TextView txtChair;
		private Button callTaxi;
		private TextView txtPrice;
	}
}
